<template>
  <view class="page">
    <!--		返回按钮-->
    <image
      class="back-btn"
      :src="backIcon"
      :style="{ top: `${backBtnPosition.top}px` }"
      @click.stop="
        () => {
          $uni.navigateBack()
        }
      "
    ></image>

    <view class="poster-container">
      <image class="poster" mode="widthFix" src="https://kou.gdzmtech.com/pic/31f1c5977d4d0d4f2e9118fbdd99be9.png"></image>
      <!--			分享按钮-->
      <button open-type="share" class="share-btn"></button>
    </view>
    <!--		邀请记录-->
    <view class="share-records-container">
      <view class="share-records">
        <view class="tab-list">
          <view
            class="tab-item"
            v-for="item in tabList"
            :key="item"
            :class="{ active: currentActiveTab === item }"
            @click="() => tabChange(item)"
            >{{ item }}</view
          >
        </view>
        <!--			邀请记录-->
        <template v-if="currentActiveTab === '邀请记录'">
          <view class="invite-record block">
            <view class="record-item" v-for="item in invitedUserList" :key="item.id">
              <view class="avatar-container">
                <image class="avatar" v-if="item.userIcon" :src="getShowImage(item.userIcon)"></image>
                <view class="avatar-text" v-else>{{ item.nickName[0] }}</view>
              </view>
              <view class="info">
                <view class="nick-name">{{ item.nickName }}</view>
                <view class="invite-time">{{ item.createTime }}</view>
              </view>
              <view class="commissions">{{ `收益+${numberUtils.fenToYuan(details.activitiesCommission)}元` }}</view>
            </view>
          </view>
          <empty height="50vh" text="暂无邀请记录" v-if="invitedUserList.length === 0"></empty>
        </template>
        <!--			提现申请-->
        <view class="withdrawal-application block" v-else-if="currentActiveTab === '提现申请'">
          <view class="info">
            <view class="available-commissions">
              <text>可提现\t</text>
              <text class="number">{{ `￥${numberUtils.fenToYuan(availableCommissions)}` }}</text>
              <text>\t元</text>
            </view>
            <view class="withdraw-btn" @click="withdraw">立即提现</view>
          </view>
          <!--					提现记录-->
          <view class="withdrawal-record">
            <view class="withdrawal-record-item" v-for="item in withDrawRecordList" :key="item.outDetailNo">
              <view class="info">
                <view class="withdraw-amount">{{ `提现 ${numberUtils.fenToYuan(item.transferAmount)} 元` }}</view>
                <view class="withdraw-time">{{ item.createTime }}</view>
              </view>
              <!--							提款状态-->
              <view class="withdrawal-status success" v-if="item.transferSuccess">已打款</view>
              <view class="withdrawal-status error" v-else>提现失败，请联系客服处理</view>
            </view>
          </view>
          <empty height="40vh" text="暂无提现申请记录" v-if="withDrawRecordList.length === 0"></empty>
        </view>
      </view>
    </view>

    <!--		客服浮窗-->
    <view class="kefu-float" @click="() => $refs.contractKeFu.open()">
      <image class="kefu-icon" :src="kefuIcon"></image>
      <view class="text">联系客服</view>
    </view>
    <contractKeFu ref="contractKeFu"></contractKeFu>
  </view>
</template>

<script>
import common from "./common"
import empty from "@/components/empty.vue"
import backIcon from "./assets/back.png"
import request from "@/utils/request"
import { getShowImage, showModal, showToast } from "@/utils"
import numberUtils from "@/utils/numberUtils"
import contractKeFu from "@/components/contractKeFu/index.vue"
import kefuIcon from "./assets/kefu.png"

export default {
  computed: {
    numberUtils() {
      return numberUtils
    },
  },
  data() {
    return {
      details: {
        activitiesCommission: 0,
      },
      currentActiveTab: "邀请记录",
      tabList: ["邀请记录", "提现申请"],
      backIcon,
      kefuIcon,
      backBtnPosition: {
        top: 0,
      },
      invitedUserList: [], // 邀请记录
      availableCommissions: 0, // 可提现的佣金
      withDrawRecordList: [], // 提现记录
			scenarioCode: "", // 当前提现场景编号，调取获取用户分享列表和提现接口会用到
    }
  },
  onLoad(query) {
    common.getDetails(query.id).then((data) => {
      this.details = data
    })
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
    this.backBtnPosition = {
      top: menuButtonInfo.top,
    }
    this.tabChange()
  },
  components: { empty, contractKeFu },
  async onShareAppMessage() {
    return await common.getShareInfo('inviteCashBack')
  },
  methods: {
    getShowImage,
    tabChange(val = this.currentActiveTab) {
      this.currentActiveTab = val
      switch (val) {
        case "邀请记录":
          this.getInvitedUserList()
          break
        case "提现申请":
          this.getCommissionsDetail()
          break
      }
    },
    //获取邀请记录
    getInvitedUserList() {
      request({
        url: "shareActivityUser/getMyShareUserPage",
        showLoading: true,
        errorHandle: true,
      }).then((data) => {
        this.invitedUserList = data.records
      })
    },
    //获取佣金详情
    getCommissionsDetail() {
      request({
        url: "userTransfers/getWithdrawalFee",
        showLoading: true,
        errorHandle: true,
      }).then((data) => {
        this.availableCommissions = data.fee
        this.withDrawRecordList = data.transfersList
				this.scenarioCode = data.scenarioCode
      })
    },
    //发起提现
    withdraw() {
      //检查可提现的金额是否为0元
      if (this.availableCommissions + "" === "0") {
        showToast.default("暂无可提现金额")
        return
      }
      //检查提现的金额是否小于1元
      if (Number.parseInt(this.availableCommissions) < 100) {
        showToast.default("提现金额不能小于1元")
        return
      }
      request({
        url: "userTransfers/withdrawal",
				data: {
					scenarioCode: this.scenarioCode,
				},
        showLoading: true,
        errorHandle: true,
      }).then(() => {
        this.getCommissionsDetail()
        showModal({
          title: "发起提现成功",
          content: "微信零钱到账可能会有延迟，请耐心等待，如有问题可联系客服",
          showCancel: false,
        })
      })
    },
  },
  //下拉刷新
  onPullDownRefresh() {
    this.tabChange()
    wx.stopPullDownRefresh()
  },
}
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  position: relative;
  .back-btn {
    width: 70rpx;
    height: 70rpx;
    position: absolute;
    left: 20rpx;
    z-index: 999;
  }
  .poster-container {
    display: flex;
    flex-direction: column;
    position: relative;
    .poster {
      width: 100vw;
    }
    .share-btn {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      opacity: 0;
    }
  }
  .share-records-container {
    padding: {
      left: 20rpx;
      right: 20rpx;
      bottom: calc(20rpx + env(safe-area-inset-bottom));
    }
    background: linear-gradient(180deg, #edb798, #fff6e4);
    width: 100vw;
    .share-records {
      background: var(--background-color);
      border-radius: 20rpx;
      box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
      padding: 30rpx 20rpx;
      height: 60vh;
      display: flex;
      flex-direction: column;
      .tab-list {
        display: flex;
        align-items: center;
        padding-bottom: 30rpx;
        width: 100%;
        .tab-item {
          width: 0;
          flex: 1;
          display: flex;
          justify-content: center;
          font-size: 36rpx;
          position: relative;
          &.active {
            font-weight: bold;
          }
          &.active:after {
            content: "";
            position: absolute;
            bottom: -12rpx;
            left: 50%;
            transform: translateX(-50%);
            width: 60rpx;
            height: 10rpx;
            border-radius: 5rpx;
            background: #fd321e;
          }
        }
      }
      > .block {
        flex: 1;
        overflow: auto;
        width: 100%;
      }
      .invite-record {
        .record-item {
          border-bottom: 2rpx solid #ccc;
          padding: 20rpx 0;
          display: flex;
          align-items: center;
          .avatar-container {
            width: 100rpx;
            height: 100rpx;
            border-radius: 50%;
            overflow: hidden;
            .avatar {
              width: 100%;
              height: 100%;
            }
            .avatar-text {
              width: 100%;
              height: 100%;
              display: flex;
              justify-content: center;
              align-items: center;
              font-size: 40rpx;
              background: #3c3c3c;
              color: white;
            }
          }
          > .info {
            flex: 1;
            padding-left: 20rpx;
            .nick-name {
              font-size: 32rpx;
            }
            .invite-time {
              font-size: 28rpx;
              color: #999;
              margin-top: 10rpx;
            }
          }
          .commissions {
            font-size: 32rpx;
            color: #fd321e;
          }
        }
      }
      .withdrawal-application {
        > .info {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-top: 20rpx;
          padding-bottom: 20rpx;
          .available-commissions {
            font-size: 34rpx;
            .number {
              font-weight: bold;
              color: #fd321e;
            }
          }
          .withdraw-btn {
            background: #fd321e;
            color: white;
            text-align: center;
            border-radius: 10rpx;
            padding: 10rpx 20rpx;
          }
        }
        .withdrawal-record {
          .withdrawal-record-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 2rpx solid #ccc;
            padding: 30rpx 0;
            > .info {
              .withdraw-amount {
                font-size: 32rpx;
              }
              .withdraw-time {
                font-size: 28rpx;
                color: #999;
              }
            }
            .withdrawal-status {
              font-size: 32rpx;
              &.success {
                color: var(--success-color);
              }
              &.error {
                color: var(--error-color);
              }
            }
          }
        }
      }
    }
  }
  .kefu-float {
    position: fixed;
    bottom: calc(50rpx + env(safe-area-inset-bottom));
    right: 20rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    width: 107rpx;
    height: 107rpx;
    .kefu-icon {
      width: 50rpx;
      height: 50rpx;
    }
    .text {
      font-size: 24rpx;
      color: #333;
      margin-left: 10rpx;
    }
    background: white;
    border-radius: 50%;
    box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
  }
}
</style>
